package com.zprmb.udzt;

import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.KeyboardEnum;
import com.zprmb.udzt.util.KeyboardEnum.ActionEnum;
import com.zprmb.udzt.util.MySharePreferenceUtil;
import com.zprmb.udzt.util.VolleryUtil;

import org.json.JSONObject;

import java.util.ArrayList;

public class V6FingerPayPwdConfirmActivity extends BaseActivity implements OnClickListener {
    private TextView pay_keyboard_one;
    private TextView pay_keyboard_two;
    private TextView pay_keyboard_three;
    private TextView pay_keyboard_four;
    private TextView pay_keyboard_five;
    private TextView pay_keyboard_sex;
    private TextView pay_keyboard_seven;
    private TextView pay_keyboard_eight;
    private TextView pay_keyboard_nine;
    private TextView pay_keyboard_zero;
    private TextView pay_keyboard_del;
    private ArrayList<String> mList = new ArrayList<String>();
    private TextView pay_box1;
    private TextView pay_box2;
    private TextView pay_box3;
    private TextView pay_box4;
    private TextView pay_box5;
    private TextView pay_box6;
    private StringBuffer sb = new StringBuffer();
    Intent intent = new Intent();
    private TextView tv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finger_confirm_pwd);
        setTitle("开启指纹支付", this);
        setgoback(this);
        tv_back = (TextView) findViewById(R.id.tv_back);
        pay_keyboard_one = (TextView) findViewById(R.id.pay_keyboard_one);
        pay_keyboard_two = (TextView) findViewById(R.id.pay_keyboard_two);
        pay_keyboard_three = (TextView) findViewById(R.id.pay_keyboard_three);
        pay_keyboard_four = (TextView) findViewById(R.id.pay_keyboard_four);
        pay_keyboard_five = (TextView) findViewById(R.id.pay_keyboard_five);
        pay_keyboard_sex = (TextView) findViewById(R.id.pay_keyboard_sex);
        pay_keyboard_seven = (TextView) findViewById(R.id.pay_keyboard_seven);
        pay_keyboard_eight = (TextView) findViewById(R.id.pay_keyboard_eight);
        pay_keyboard_nine = (TextView) findViewById(R.id.pay_keyboard_nine);
        pay_keyboard_zero = (TextView) findViewById(R.id.pay_keyboard_zero);
        pay_keyboard_del = (TextView) findViewById(R.id.pay_keyboard_del);
        pay_box1 = (TextView) findViewById(R.id.pay_box1);
        pay_box2 = (TextView) findViewById(R.id.pay_box2);
        pay_box3 = (TextView) findViewById(R.id.pay_box3);
        pay_box4 = (TextView) findViewById(R.id.pay_box4);
        pay_box5 = (TextView) findViewById(R.id.pay_box5);
        pay_box6 = (TextView) findViewById(R.id.pay_box6);
        pay_keyboard_one.setOnClickListener(this);
        pay_keyboard_two.setOnClickListener(this);
        pay_keyboard_three.setOnClickListener(this);
        pay_keyboard_four.setOnClickListener(this);
        pay_keyboard_five.setOnClickListener(this);
        pay_keyboard_sex.setOnClickListener(this);
        pay_keyboard_seven.setOnClickListener(this);
        pay_keyboard_eight.setOnClickListener(this);
        pay_keyboard_nine.setOnClickListener(this);
        pay_keyboard_zero.setOnClickListener(this);
        pay_keyboard_del.setOnClickListener(this);
    }

    public void checkPwd() {
        AjaxParams params = new AjaxParams();
        params.put("payPass", sb.toString());
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    Editor editor = getShare().edit();

                    editor.putInt(getMobile() + "fingerPay", 1);

                    editor.commit();
                    setResult(2, intent);
                    MySharePreferenceUtil.put(V6FingerPayPwdConfirmActivity.this, "zhiwen", "3");
                    finish();
                } else {
                    Toast.makeText(V6FingerPayPwdConfirmActivity.this, jsonObject.optString("comment"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
        volleryUtil.setRequestMethod(1);
        volleryUtil.setShow(true);
        volleryUtil.execute(V6FingerPayPwdConfirmActivity.this, Constant.URL + "api/v6/invest/checkPayPwd?", params.toString());

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.pay_keyboard_one:
                //et_pay_money.setSelection(sb_money.length());


                parseActionType(KeyboardEnum.one);


                break;
            case R.id.pay_keyboard_two:
                //et_pay_money.setSelection(sb_money.length());
                parseActionType(KeyboardEnum.two);


                break;
            case R.id.pay_keyboard_three:
                //et_pay_money.setSelection(sb_money.length());

                parseActionType(KeyboardEnum.three);


                break;
            case R.id.pay_keyboard_four:
                //et_pay_money.setSelection(sb_money.length());

                parseActionType(KeyboardEnum.four);

                break;
            case R.id.pay_keyboard_five:
                //et_pay_money.setSelection(sb_money.length());

                parseActionType(KeyboardEnum.five);

                break;
            case R.id.pay_keyboard_sex:
                //et_pay_money.setSelection(sb_money.length());

                parseActionType(KeyboardEnum.sex);

                break;
            case R.id.pay_keyboard_seven:
                //et_pay_money.setSelection(sb_money.length());

                parseActionType(KeyboardEnum.seven);

                break;
            case R.id.pay_keyboard_eight:
                //et_pay_money.setSelection(sb_money.length());
                parseActionType(KeyboardEnum.eight);

                break;
            case R.id.pay_keyboard_nine:
                //et_pay_money.setSelection(sb_money.length());

                parseActionType(KeyboardEnum.nine);

                break;
            case R.id.pay_keyboard_zero:
                //et_pay_money.setSelection(sb_money.length());

                parseActionType(KeyboardEnum.zero);

                break;
            case R.id.pay_keyboard_del:
                //et_pay_money.setSelection(sb_money.length());
                parseActionType(KeyboardEnum.del);

        }


    }

    private void updateUi() {
        if (mList.size() == 0) {
            pay_box1.setText("");
            pay_box2.setText("");
            pay_box3.setText("");
            pay_box4.setText("");
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 1) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText("");
            pay_box3.setText("");
            pay_box4.setText("");
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 2) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText("");
            pay_box4.setText("");
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 3) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText(mList.get(2));
            pay_box4.setText("");
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 4) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText(mList.get(2));
            pay_box4.setText(mList.get(3));
            pay_box5.setText("");
            pay_box6.setText("");
        } else if (mList.size() == 5) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText(mList.get(2));
            pay_box4.setText(mList.get(3));
            pay_box5.setText(mList.get(4));
            pay_box6.setText("");
        } else if (mList.size() == 6) {
            pay_box1.setText(mList.get(0));
            pay_box2.setText(mList.get(1));
            pay_box3.setText(mList.get(2));
            pay_box4.setText(mList.get(3));
            pay_box5.setText(mList.get(4));
            pay_box6.setText(mList.get(5));
        }
    }

    private void parseActionType(KeyboardEnum type) {
        if (type.getType() == ActionEnum.add) {
            if (mList.size() < 6) {
                mList.add(type.getValue());
                updateUi();
                if (mList.size() == 6) {
                    sb.delete(0, sb.length());
                    for (int i = 0; i < mList.size(); i++) {
                        sb.append(mList.get(i));
                    }
                    checkPwd();
//					if (tv_v4_pay_paypass_status.getText().toString().equals("请输入支付密码")) {

//					} else if (tv_v4_pay_paypass_status.getText().toString().equals("请设置交易密码")) {
//						tv_v4_pay_paypass_status.setText("请确认交易秘密");
//						pass1 = sb_set_pass_1.toString();
//						sb_set_pass_1.delete(0, sb_set_pass_1.length());
//						mList.clear();
//						updateUi();
//						if (pass1.equals(sb_set_pass_1.toString())) {
////							initSetPass();
//							ToastUtil.showCenterToast(this, "设置密码");
//						} else {
//							ToastUtil.showCenterToast(this, "两次输入的交易密码不一致，请重新输入");
//						}
//				}
                }
            } else {
//				for (int i = 0; i < mList.size(); i++) {
//					sb.append(mList.get(i));
//				}
//				invest();
            }
        } else if (type.getType() == ActionEnum.delete) {
            if (mList.size() > 0) {
                mList.remove(mList.get(mList.size() - 1));
                updateUi();
            }
        } else if (type.getType() == ActionEnum.cancel) {
            // listener.onCancelPay();
        } else if (type.getType() == ActionEnum.sure) {
            /*
             * if(mList.size()<6){ Toast.makeText(this, "交易密码必须6位",
			 * Toast.LENGTH_SHORT).show(); }else{ //String payValue=""; for (int
			 * i = 0; i < mList.size(); i++) { //payValue+=mList.get(i);
			 * sb.append(mList.get(i)); } //listener.onSurePay(payValue); }
			 */
        } else if (type.getType() == ActionEnum.longClick) {
            // mList.clear();
            // updateUi();
        }
    }
}
